package com.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.constant.MessageConstant;
import com.exception.CustomException;
import com.pageResult.PageResult;
import com.pojo.Permission;
import com.query.QueryPageBean;
import com.result.Result;
import com.service.PermissionService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /*删除*/
    @PostMapping("/delete")
    public Result delete(Integer id) throws CustomException {
        permissionService.delete(id);
        return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Permission Permission){
        try {
            permissionService.update(Permission);
            return new Result(true,MessageConstant.EDIT_PERMISSION_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
    }


    //添加
    @PostMapping("/add")
    public Result add(@RequestBody  Permission Permission){
        try {
            permissionService.add(Permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
    }


    //根据id查询
    @RequestMapping("/queryById")
    public Result queryById(Integer id){
        try {
            Permission permission = permissionService.queryById(id);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS,permission);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
    }




    //查询检查组（分页+条件）
    @PostMapping("/queryPage")
    public Result queryPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult result = permissionService.selectByQueryString(queryPageBean);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
    }




    //权限 列表(不支持分页)
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Permission> list = permissionService.queryAll();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
    }
}
